package francis.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.SimpleAdapter.ViewBinder;
import francis.model.VideoInfo;
import francis.utils.VideoUtil;
import francis.utils.WebTools;

public class ListVideoActivity extends Activity {

	ListView view;
	List<Map<String, Object>> data;

	/**
	 * map参数：
	 * header_text: 标题
	 * url: xml地址
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/*
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 全屏幕
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题，必须在setContentView之前执行
		*/
		@SuppressWarnings("unchecked")
		HashMap<String, String> key_value = (HashMap<String, String>) this
				.getIntent().getSerializableExtra("key_value");
		

		this.setContentView(R.layout.list);
		((TextView) this.findViewById(R.id.header_text)).setText(key_value.get("header_text"));

		view = (ListView) this.findViewById(R.id.video_list);

		data = new ArrayList<Map<String, Object>>();
		//获取VideoInfo信息
		List<VideoInfo> vInfos = VideoUtil
				.getVideoInfos(key_value.get("url"));
		for (VideoInfo info : vInfos) {

			Bitmap bitmap = WebTools.returnBitMap(info.getPic());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("url", bitmap);
			map.put("title", info.getTitle());
			map.put("tag", info.getAuthor() + " " + info.getCreate());
			data.add(map);
		}

		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.list_item, new String[] { "url", "title", "tag" },
				new int[] { R.id.list_img, R.id.list_title, R.id.list_tag });

		adapter.setViewBinder(new ViewBinder() {
			@Override
			public boolean setViewValue(View view, Object data,
					String textRepresentation) {

				if (view instanceof ImageView && data instanceof Bitmap) {
					ImageView iv = (ImageView) view;
					iv.setImageBitmap((Bitmap) data);
					return true;
				}
				return false;
			}
		});

		view.setAdapter(adapter);
	}

}
